<template>
  <el-row>
    <el-col :span="24">
      <form class="el-form query-form el-form--label-right el-form--inline">
        <div class="el-form-item el-form-item--medium" v-if="!(admin.mType >= 4)">
          <label for="date" class="el-form-item__label" style="width: 60px;">街道</label>
          <div class="el-form-item__content">
            <el-select
              v-model="query.zhenId"
              clearable
              placeholder="请选择想要查看的街道"
              @change="get_villageAtt()"
            >
              <el-option
                v-for="(town, index) in townAtt"
                :key="index"
                :label="town.zName"
                :value="town.zhenId"
              ></el-option>
            </el-select>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium" v-if="!(admin.mType == 5)">
          <label for="date" class="el-form-item__label" style="width: 60px;">村</label>
          <div class="el-form-item__content">
            <el-select
              v-model="query.villageId"
              clearable
              placeholder="请选择想要查看的村"
              @change="get_groupAtt()"
            >
              <el-option
                v-for="(village, index) in villageAtt"
                :key="index"
                :label="village.vName"
                :value="village.villageId"
              ></el-option>
            </el-select>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium">
          <label for="date" class="el-form-item__label" style="width: 80px;">用户名</label>
          <div class="el-form-item__content">
            <el-input placeholder="请输入内容" v-model="query.mAccountnumber" clearable></el-input>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium ml-6">
          <!---->
          <div class="el-form-item__content">
            <el-button type="primary" icon="el-icon-search" @click="to_query">搜索</el-button>
          </div>
        </div>
        <div class="el-form-item el-form-item--medium ml-9" v-if="!(admin.mType == 3)">
          <div class="el-form-item__content">
            <el-button plain icon="el-icon-plus" @click="show_form()">
              <span>添加</span>
            </el-button>
          </div>
          <!---->
        </div>
      </form>
    </el-col>
    <el-col :span="24">
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(225, 225, 225, 0.8)"
        ref="multipleTable"
        :data="items"
        border
        tooltip-effect="dark"
        style="width: 100%"
        max-height="600"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="120" align="center"></el-table-column>
        <el-table-column type="index" width="120" align="center"></el-table-column>
        <el-table-column label="账号" width="240" align="center" prop="mAccountnumber"></el-table-column>
        <el-table-column label="用户名" width="240" align="center" prop="mName"></el-table-column>
        <el-table-column label="手机号" width="240" align="center" prop="mAccountnumber"></el-table-column>
        <el-table-column label="所属街道" width="200" align="center" prop="Z_Name"></el-table-column>
        <el-table-column label="角色" width="140" align="center" prop="M_Type"></el-table-column>
        <el-table-column label="编辑" align="center" show-overflow-tooltip>
          <template slot-scope="scope" v-if="$route.path === '/admin'">
            <el-button type="primary" icon="el-icon-tickets" @click="updata(scope.$index)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-row class="d-flex jc-end">
          <el-col :span="12" class="pr-6">
            <div class="grid-content bg-purple-light">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.current_page"
                :page-sizes="pagination.page_sizes"
                :page-size="pagination.page_size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total"
              ></el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
      <el-dialog
        title="管理员详情"
        width="25%"
        :show-close="true"
        :close-on-click-modal="false"
        :lock-scroll="true"
        :visible.sync="dialogFormVisible_edmit"
      >
        <el-form
          ref="form"
          :model="form"
          label-width="7.5rem"
          class="d-flex flex-column w-10 p-2 pt-0"
        >
          <el-col :span="24">
            <el-col :span="24" class="d-flex flex-column">
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">用户姓名</label>
                <div class="el-form-item__content flex-1">{{form.mName}}</div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">登陆账号</label>
                <div class="el-form-item__content flex-1">{{form.mAccountnumber}}</div>
              </div>

              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">职位</label>
                <div class="el-form-item__content flex-1">{{form.J_name}}</div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">角色</label>
                <div class="el-form-item__content flex-1">{{form.M_Type}}</div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">市</label>
                <div class="el-form-item__content flex-1">{{form.S_name}}</div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">区</label>
                <div class="el-form-item__content flex-1">{{form.R_name}}</div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">街道</label>
                <div class="el-form-item__content flex-1">{{form.Z_Name}}</div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">村</label>
                <div class="el-form-item__content flex-1">{{form.V_Name}}</div>
              </div>
            </el-col>
          </el-col>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel_u">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="管理员添加"
        width="25%"
        :show-close="true"
        :close-on-click-modal="false"
        :lock-scroll="true"
        :visible.sync="dialogFormVisible_create"
      >
        <el-form
          ref="form"
          :model="form"
          label-width="7.5rem"
          class="d-flex flex-column w-10 p-2 pt-0"
        >
          <el-col :span="24">
            <el-col :span="24" class="d-flex flex-column">
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">管理员级别</label>
                <div class="el-form-item__content">
                  <el-select v-model="form.mType" clearable placeholder="请选择权限">
                    <el-option
                      v-for="(item, index) in typeAtt"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">街道</label>
                <div class="el-form-item__content">
                  <el-select
                    v-model="form.mZhenid"
                    clearable
                    placeholder="请选择想要查看的街道"
                    @change="get_villageAtt(true)"
                  >
                    <el-option
                      v-for="(town, index) in townAtt_form"
                      :key="index"
                      :label="town.zName"
                      :value="town.zhenId"
                    ></el-option>
                  </el-select>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">村</label>
                <div class="el-form-item__content">
                  <el-select v-model="form.mVillageid" clearable placeholder="请选择想要查看的村">
                    <el-option
                      v-for="(village, index) in villageAtt_form"
                      :key="index"
                      :label="village.vName"
                      :value="village.villageId"
                    ></el-option>
                  </el-select>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">登陆账号</label>
                <div class="el-form-item__content">
                  <el-input v-model="form.mAccountnumber" clearable></el-input>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">登陆密码</label>
                <div class="el-form-item__content">
                  <el-input v-model="form.mPassword" clearable></el-input>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">用户姓名</label>
                <div class="el-form-item__content">
                  <el-input v-model="form.mName" clearable></el-input>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">性别</label>
                <div class="el-form-item__content">
                  <el-select v-model="form.mSex" clearable placeholder="请选择用户性别">
                    <el-option
                      v-for="(person, index) in [{name: '男', value: 0}, {name: '女', value: 1}]"
                      :key="index"
                      :label="person.name"
                      :value="person.value"
                    ></el-option>
                  </el-select>
                </div>
              </div>
              <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                <label for="date" class="el-form-item__label" style="width: 7.5rem;">电话</label>
                <div class="el-form-item__content">
                  <el-input v-model="form.mPhone" clearable></el-input>
                </div>
              </div>
            </el-col>
          </el-col>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel_c">取 消</el-button>
          <el-button type="primary" @click="reset_c">重 置</el-button>
          <el-button type="primary" @click="create">确 定</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>


<script>
export default {
  data() {
    return {
      model: {},
      admin: {},
      obj: {},
      items: [
        //表格内容
      ],
      init_items: [
        //原始表格渲染数据
      ],
      pagination: {
        current_page: 1,
        page_sizes: [20, 30, 50, 100],
        page_size: 20,
        total: 0,
        page_length: 0
      },
      loading: true,
      formLabelWidth: "100px",
      form: {
        //弹出框内容
      },
      query: {
        //检索对象
        method: true,
        data: {}
      },
      //序列id
      index_id: 0,
      index: 0,
      //对应村
      villageAtt: [],
      villageAtt_form: [],
      //对应镇
      townAtt: [],
      townAtt_form: [],
      //对应组
      groupAtt: [],
      groupAtt_form: [],
      //多删数组
      delAtt: [],
      multipleSelection: [],
      //文件上传
      fileList: [],
      //删除的对象
      del_obj: {},
      delMany: {},
      //模态框
      dialogFormVisible_create: false,
      dialogFormVisible_edmit: false,
      typeAtt: [{label:'领导', value: 1},{label:'区级管理员', value: 2},{label:'其他职能部门管理员', value: 3},{label:'镇级管理员', value: 4},{label:'村级管理员', value: 5}]
    };
  },

  methods: {
    setQuery() {
      if (this.admin.mType == 4) {
        this.query.zhenId = this.admin.mZhenid;
        this.get_villageAtt();
      }
      if (this.admin.mType == 5) {
        this.query.zhenId = this.admin.mZhenid;
        this.query.villageId = this.admin.mVillageid;
      }
    },
    setObj(obj) {
      obj.startPage = this.pagination.current_page;
      obj.pagesize = this.pagination.page_size;
    },
    async get_townAtt(flag) {
      let res;
      if (flag) {
        res = await this.$http.get(`/caiwu/findzhenByRegionId/1349`);
        this.townAtt_form = res.data.data;
      } else {
        res = await this.$http.get(`/caiwu/findzhenByRegionId/1349`);
        this.townAtt = res.data.data;
      }
    },
    async get_villageAtt(flag) {
      let res;
      if (flag) {
        res = await this.$http.get(
          `/caiwu/findzhenByZhenId/${this.form.mZhenid}`
        );
        this.villageAtt_form = res.data.data;
      } else {
        res = await this.$http.get(
          `/caiwu/findzhenByZhenId/${this.query.zhenId}`
        );
        this.villageAtt = res.data.data;
      }
    },
    async get_groupAtt(flag) {
      let res;
      if (flag) {
        res = await this.$http.get(`/caiwu/findZu/${this.form.mVillageid}`);
        this.groupAtt_form = res.data.data;
      } else {
        res = await this.$http.get(`/caiwu/findZu/${this.query.mVillageid}`);
        this.groupAtt = res.data.data;
      }
    },
    //请求数据
    async fetch(obj) {
      this.setObj(obj);
      const res = await this.$http.post("/h/managers", this.Qs.stringify(obj));
      this.init_items = res.data;
      this.items = res.data.data;
      this.up_pagination();
      if (
        this.$util.getCookie("current_page") !==
        String(this.pagination.current_page)
      ) {
        this.handleCurrentChange(this.$util.getCookie("current_page"));
      }
      if (res.status == 200) {
        this.loading = false;
      }
    },
    handleSelectionChange(val) {
      let att = [];
      this.multipleSelection = val;
      this.multipleSelection.forEach(item => {
        let { H_Key } = item;
        att.push(H_Key);
      });
      this.delAtt = att;
    },
    setIndex() {
      this.$basicMes.forEach((item, index) => {
        if (item.name == this.query.town) {
          this.villageAtt = this.$basicMes[index].village;
        }
      });
    },
    get_delObj() {
      let { managerId } = this.admin;
      this.del_obj = { managerId };
    },
    get_delMany() {
      this.delMany = this.delAtt;
    },
    del(index, name) {
      this.get_delObj();
      this.index_id = index;
      this.form = this.items[index];
      this.del_obj.capitalDetailsId = this.form.CapitalDetails_Id;
      this.$confirm(
        `此操作将永久删除 ${this.items[index][name]}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }
      )
        .then(async index => {
          const res = await this.$http.delete(
            `/HuDeleteone/${this.form.H_Key}/${this.admin.managerId}`
          );
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.fetch(this.obj);
          } else {
            this.$message({
              type: "error",
              message: "删除失败!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    updata(index) {
      // alert("hahah");
      this.index_id = index;
      this.form = this.items[index];
      this.dialogFormVisible_edmit = true;
    },
    async del_multip(name) {
      if (!this.delAtt.length) {
        return;
      }
      this.get_delMany();
      let str = "";
      this.multipleSelection.forEach(item => {
        str += item[name] + "\t\t";
      });

      this.$confirm(`此操作将永久删除 ${str}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(async index => {
          const res = await this.$http.delete(
            `/HuDeleteMany/${this.delAtt.toString()}/${this.admin.managerId}`
          );
          console.log(".....////");
          console.log(res);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.fetch(this.obj);
          } else {
            this.$message({
              type: "error",
              message: "删除失败!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    //选择文件
    select_file() {
      let path = this.$route.name;
    },
    //文件上传
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    //分页功能
    up_pagination() {
      this.pagination.page_length = Math.ceil(
        this.init_items.count / this.pagination.current_page
      );
      this.pagination.total = this.init_items.count;
    },
    handleSizeChange(val) {
      this.pagination.page_size = val;
      this.pagination.current_page = 1;
    },
    async handleCurrentChange(val) {
      this.pagination.current_page = val;
      this.$util.setCookie("current_page", this.pagination.current_page);
      this.fetch(this.obj);
    },
    //query
    to_query() {
      this.fetch(this.query);
    },

    //create
    reset_c() {
      this.$confirm("确定重置新增数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(() => {
        this.$message({
          type: "success",
          message: "已重置"
        });
        this.form = {};
      });
    },
    show_form() {
      this.get_townAtt(true);
      this.dialogFormVisible_create = true;
    },

    cancel_c() {
      this.$confirm("取消添加将不会保存该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已取消！"
          });
          this.form = {};
          this.dialogFormVisible_create = false;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    success_c(res, file, files) {},

    async create() {
      // this.set_form(this.form);
      let defaultObj = {
        mDepartmentid: "1",
        mPost: "1",
        mRegion: "1349",
        mShi: "151"
      };
      this.form = Object.assign(defaultObj, this.form);
      const res = await this.$http.post(
        `/h/manager`,
        this.form
      );
      if (res.data.state == 'success') {
        this.$message({
          type: "success",
          message: '添加成功'
        });
        this.fetch(this.obj);
        this.dialogFormVisible_create = false;
      }else {
        this.$message({
          type: "warning",
          message: '添加失败'
        });
      }
    },

    reset_u() {
      this.$confirm("确定重置编辑数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(() => {
        this.$message({
          type: "success",
          message: "已重置"
        });
        for (let item in this.form) {
          if (item !== "_id") {
            this.form[item] = "";
          }
        }
      });
    },
    cancel_u() {
      this.$confirm("取消添加将不会保存该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已取消！"
          });
          setTimeout(() => {
            this.dialogFormVisible_edmit = false;
          }, 200);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },

    async updata_save() {
      this.form.managerId = this.admin.managerId;
      this.form.hKey = this.form.H_Key;
      this.form.hName = this.form.H_name;
      const res = await this.$http.post(
        "/HuUpdate",
        this.Qs.stringify(this.form)
      );
      if (res.status == 200) {
        this.dialogFormVisible_edmit = false;
        this.fetch(this.obj);
        this.$message({
          message: "编辑成功",
          type: "success"
        });
        this.fetch(this.obj);
      } else {
        this.$message({
          message: "编辑失败",
          type: "error"
        });
      }
    },
    setTypeAtt() {
      if(this.admin.mType === 2) {
        this.typeAtt = [{label:'其他职能部门管理员', value: 3},{label:'镇级管理员', value: 4},{label:'村级管理员', value: 5}]
      }
      if(this.admin.mType === 4) {
        this.typeAtt = [{label:'村级管理员', value: 5}]
      }
    },
  },
  created() {
    this.admin = this.$util.handleLocalStorage("get", "admin");

    this.get_townAtt();
    this.pagination.current_page = 1;
    this.$util.setCookie("current_page", this.pagination.current_page);
    this.setQuery();
    this.fetch(this.obj);
  }
};
</script>

<style lang="scss" scoped>
.query-form {
  padding-top: 20px;
  margin-bottom: 20px;
  background-color: #f2f2f2;
  display: flex;
  justify-content: flex-start;
}
.el-form--inline .el-form-item {
  display: inline-block;
  margin-right: 10px;
  vertical-align: top;
}
</style>

